package com.wanmait.deliverymangementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.mapper.RunnerMapper;
import com.wanmait.deliverymangementsystem.pojo.Runner;
import com.wanmait.deliverymangementsystem.service.NicknameService;
import com.wanmait.deliverymangementsystem.service.RunnerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RunnerServiceImpl implements RunnerService {
    @Resource
    RunnerMapper runnerMapper;
    @Resource
    NicknameService nicknameService;
    @Override
    public Runner Login(Runner runner) {
        runner=runnerMapper.selectByPhone_numberAndPassword(runner);
        if (runner!=null)
        runnerMapper.updateLastLoginTimeByID(runner.getId());
        return runner;

    }

    @Override
    public List<Runner> runnerList() {
        return runnerMapper.findAll();
    }

    @Override
    public void register(Runner runner) {
        runnerMapper.insert(runner);
    }

    @Override
    public void taskAvailable(Boolean ava, Integer id)  {
        runnerMapper.updateAvailable(ava,id);
    }

    @Override
    public void addOrderNumber(Runner runner) {
        runnerMapper.updateCompletedOrderNumberByID(runner.getId(), runner.getCompletedOrderNumber());
    }

    @Override
    public void updateWallet(Runner runner) {
        runnerMapper.updateWalletByID(runner);
    }

    @Override
    public void updateLoginStauts(Integer id, Boolean status) {
        runnerMapper.updateEnableByID(id,status);
    }

    @Override
    public void giveNickName(Integer id, Integer nickID) {
        runnerMapper.updateNickNameByID(id,nickID);
    }

    @Override
    public Runner findById(Integer id) {
        return runnerMapper.selectByID(id);
    }

    @Override
    public void updateRatingById(Runner runner) {
        runnerMapper.updateRatingByID(runner);
    }

    @Override
    public PageInfo<Runner> findByCityID(Integer page, Integer id) {

        if (page==null)
            page=0;
        PageHelper.startPage(page,5);
        List<Runner> runnerList =runnerMapper.findByCityID(id);
        return new PageInfo<>(runnerList,5);
    }

    @Override
    public PageInfo<Runner> findRunner(Integer page,String name, String number) {
        List<Runner> runnerList;
        if (!name.isEmpty()&&number.isEmpty())
        {
            PageHelper.startPage(page,100);
            runnerList =runnerMapper.findByRunnerName(name);
        }
        if (!name.isEmpty()&&!number.isEmpty()){
            PageHelper.startPage(page,100);
            runnerList =runnerMapper.findByPhoneNumberAndName(number,name);
        }
       else
        {
            PageHelper.startPage(page,100);
            runnerList =runnerMapper.findByPhoneNumber(number);

        }
        for (Runner runner : runnerList) {
            runner.setNickname(nicknameService.selectByPrimaryKey(runner.getNickname().getId()));
        }
        return new PageInfo<>(runnerList,5);
    }
}
